<template>
	<view class="register">
		<view class="head">
			<uni-icons type="back" size="30" color="#454545" class="icon_back" @click="navigBack"></uni-icons>
		</view>

		<view class="body" v-show="account == null">
			<view class="form">
				<text>注册用户</text>
				<view class="create">
					<view class="circle" @click="setAvatar">
						<uni-icons type="plusempty" size="30" color="#9c9c9c" class="icon"></uni-icons>
						<image :src="imagefile" class="img"></image>
					</view>
					<view class="text">
						<text>请选择你的头像</text>
						<text class="two">(不选择则为默认)</text>
					</view>
				</view>
				<view class="input">
					<input class="i_put" type="text" v-model="nickName" maxlength="15" placeholder="请输入你的昵称" />
				</view>
				<view class="input">
					<input class="i_put" :type="pwdType" v-model="pwd" maxlength="15"
						placeholder="请输入你的密码(不低于6位,不超出12位)" @input="handleInput" />
					<view v-show="pwd != ''">
						<uni-icons type="eye-slash" size="25" v-show="!seeing" class="icon"
							@click="updatePwdType"></uni-icons>
						<uni-icons type="eye" size="25" v-show="seeing" class="icon" @click="updatePwdType"></uni-icons>
					</view>
				</view>
				<view class="input">
					<input class="i_put" :type="pwdType2" v-model="pwd2" maxlength="15" placeholder="确认密码"
						@input="handleInput2" />
					<view v-show="pwd2 != ''">
						<uni-icons type="eye-slash" size="25" v-show="!seeing2" class="icon"
							@click="updatePwdType2"></uni-icons>
						<uni-icons type="eye" size="25" v-show="seeing2" class="icon"
							@click="updatePwdType2"></uni-icons>
					</view>
				</view>
				<view class="input">
					<view class="item" @click="check">
						<view class="kong"
							:style="radio!=null?{backgroundColor:(radio.value === 1)?'rgba(87, 177, 255, 0.5)':'rgba(255, 14, 171, 0.5)'}:{}">
							<image :src="radio.url" v-if="radio!=null"></image>
							<text v-show="radio==null">请选择</text>
						</view>
					</view>
					<view class="drawer">
						<view class="check"
							:style="{backgroundColor:(i === 0)?'rgba(87, 177, 255, 0.5)':'rgba(255, 14, 171, 0.5)'}"
							v-for="(item,i) in sex" :key="i" @click="checkgender(i)">
							<image :src="item.url"></image>
						</view>
					</view>
				</view>
				<view class="input">
					<input class="i_put" type="text" v-model="yzm" maxlength="15" placeholder="请输入验证码" />
					<image :src="yzmSrc" @click="captchaImg"></image>
				</view>
			</view>
			<view class="buom" @click="myRegister">
				<view class="back"></view>
				<text>注册</text>
			</view>
		</view>
		<register-success :account="account" v-show="account != null"></register-success>
	</view>
</template>

<script>
	import {
		mapMutations,
	} from 'vuex'
	import {
		login,
		register,
		captcha,
		updateAvatar
	} from '../../common/api';
	export default {
		data() {
			return {
				seeing: false,
				seeing2: false,
				pwdType: 'password',
				pwdType2: 'password',
				pwd: '',
				pwd2: '',
				nickName: '',
				yzm: '',
				yzmSrc: null,
				imagefile: '',
				radio: null,
				radioX: '-242rpx',
				sex: [{
					url: '../../static/my-icons/man.png',
					value: 1
				}, {
					url: '../../static/my-icons/girl.png',
					value: 0
				}],
				account: null
			};
		},
		created() {
			this.captchaImg();
		},
		methods: {
			...mapMutations('info', ['CreateMyInfo']),
			navigBack() {
				uni.navigateBack();
			},
			updatePwdType() {
				this.seeing = !this.seeing;
				this.pwdType = (this.pwdType == 'password') ? 'text' : 'password';
			},
			updatePwdType2() {
				this.seeing2 = !this.seeing2;
				this.pwdType2 = (this.pwdType2 == 'password') ? 'text' : 'password';
			},
			handleInput(event) {
				const value = event.detail.value.replace(/[\u4e00-\u9fa5]/g, '');
				this.pwd = value;
			},
			handleInput2(event) {
				const value = event.detail.value.replace(/[\u4e00-\u9fa5]/g, '');
				this.pwd2 = value;
			},
			check() {
				if (this.radioX === '0') {
					this.radioX = '-242rpx';
					return;
				}
				this.radioX = '0';
			},
			checkgender(i) {
				this.radio = this.sex[i];
				this.check();
			},
			captchaImg() {
				captcha().then((res) => {
					let base64String = uni.arrayBufferToBase64(res);
					this.yzmSrc = `data:image/png;base64,${base64String}`;
				})
			},
			setAvatar() {
				uni.chooseImage({
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						this.imagefile = tempFilePaths[0];
					}
				});
			},
			myRegister() {
				if (this.nickName == '') {
					uni.showToast({
						title: '请输入昵称',
						duration: 1000,
						icon: 'error'
					});
					return;
				}
				if (this.pwd != this.pwd2 || this.pwd.length < 6 || this.pwd.length > 12) {
					uni.showToast({
						title: '密码不正确',
						duration: 1000,
						icon: 'error'
					});
					return;
				}
				if (this.radio == null) {
					uni.showToast({
						title: '请选择性别',
						duration: 1000,
						icon: 'error'
					});
					return;
				}
				if (this.yzm.length != 4) {
					uni.showToast({
						title: '验证码不正确',
						duration: 1000,
						icon: 'error'
					});
					this.captchaImg();
					return;
				}
				register({
					nickName: this.nickName,
					password: this.pwd,
					gender: this.radio.value,
					captcha: this.yzm,
					avatar: this.imagefile
				}).then((res) => {
					if (res.code == 200) {
						updateAvatar({path:this.imagefile,id:res.data.id}).then((ures)=>{
							let ure = JSON.parse(ures);
							if(ure.code == 200){
								this.account = res.data.account;
							}
						})
					}
					if (res.code == 302) {
						uni.showToast({
							title: '验证码不正确',
							duration: 1000,
							icon: 'error'
						});
						this.captchaImg();
						return;
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	.register {
		.head {
			width: 750rpx;
			height: 100rpx;
			padding: 50rpx 30rpx;

			.icon_back {
				background-color: #dddddd;
				border-radius: 50rpx;
				padding: 10rpx;
			}
		}

		.body {
			width: 750rpx;
			height: 400rpx;
			display: flex;
			flex-direction: column;
			align-items: center;

			.form {
				height: 200rpx;

				text {
					font-size: 38rpx;
					font-weight: bold;
					color: #4c4c4c;
				}

				.create {
					display: flex;
					align-items: center;
					padding-top: 25rpx;

					.circle {
						width: 200rpx;
						height: 200rpx;
						border-radius: 100rpx;
						background-color: #e6e6e6;
						display: flex;
						justify-content: center;
						align-items: center;
						margin: 10rpx 0;

						.img {
							position: absolute;
							width: 200rpx;
							height: 200rpx;
							border-radius: 100rpx;
						}
						.icon{
							
						}

						&:active {
							transform: scale(0.9);
						}

					}

					text {
						font-size: 30rpx;
						color: #5a5a5a;
						font-weight: bold;
						letter-spacing: 3rpx;
						// padding: 0 20rpx;
						padding-right: 0;
					}

					.text {
						display: flex;
						flex-direction: column;
						padding-left: 20rpx;
						.two {
							font-size: 25rpx;
						}
					}
				}

				.input {
					width: 500rpx;
					height: 100rpx;
					border-bottom: 1rpx solid #797979;
					display: flex;
					align-items: center;

					.i_put {
						width: 450rpx;
						height: 100rpx;
						font-size: 32rpx;
						padding: 10rpx;
					}

					image {
						width: 300rpx;
						height: 80rpx;
					}

					.item {
						background-color: #e6e6e6;
						z-index: 2;

						.kong {
							width: 240rpx;
							height: 80rpx;
							border: 1rpx solid #757575;
							display: flex;
							justify-content: center;
							align-items: center;
							background-color: rgba(14, 130, 246, 0.3);

							text {
								font-size: 28rpx;
							}

							image {
								width: 60rpx;
								height: 60rpx;
							}
						}
					}

					.drawer {
						width: 220rpx;
						display: flex;
						justify-content: space-between;
						padding-left: 20rpx;
						border: 1rpx solid #5a5a5a;
						transform: translateX(v-bind(radioX));
						transition: transform 0.3s;

						image {
							width: 60rpx;
							height: 60rpx;
						}

						.check {
							width: 100rpx;
							height: 80rpx;
							display: flex;
							align-items: center;
							justify-content: center;
							border-left: 1rpx solid #5a5a5a;

							&:active {
								transform: scale(0.9);
							}
						}
					}
				}
			}

			.buom {
				margin-top: 100rpx;
				width: 300rpx;
				height: 80rpx;
				border: 1rpx solid lightseagreen;
				display: flex;
				justify-content: center;
				align-items: center;
				overflow: hidden;
				position: fixed;
				top: 1000rpx;
				// bottom: 400rpx;
				font-weight: bold;
				font-size: 32rpx;
				border-radius: 10rpx;

				&:active {
					transform: scale(0.9);
				}

				text {
					position: fixed;
				}

				.back {
					border: 1rpx solid rebeccapurple;
					position: absolute;
					width: 600rpx;
					height: 200rpx;
					background: linear-gradient(90deg, rgba(65, 254, 179, 1) 0%, rgba(38, 238, 244, 1) 50%, rgba(14, 130, 246, 1) 100%);

					animation: move 8s infinite linear alternate;

					@keyframes move {
						from {
							transform: translateX(100rpx);
						}

						to {
							transform: translateX(-100rpx);
						}
					}
				}
			}
		}
	}
</style>